package org.emet.service;

import java.util.List;

import org.emet.dao.BusinessTeamDao;
import org.emet.entity.biz.BusinessTeam;
import org.emet.jee.BusinessException;
import org.emet.jee.core.dao.Page;
import org.emet.service.dto.BusinessTeamCriteria;
import org.hibernate.ObjectNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RemotingDestination
public class BusinessTeamService {
	@Autowired
	private BusinessTeamDao businessTeamDao;
	private static Logger logger = LoggerFactory.getLogger(BusinessTeamService.class);

	public void save(BusinessTeam entity) {
		boolean count = businessTeamDao.isExists(entity);
		if (count)
			throw new BusinessException("已经存在相同的编码或名称！");
		try {
			businessTeamDao.save(entity);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	public List<BusinessTeam> getAll() {
		try {
			return businessTeamDao.getAll();
		} catch (ObjectNotFoundException e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}
	public BusinessTeam get(Long id) {
		try {
			return businessTeamDao.get(id);
		} catch (ObjectNotFoundException e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	public void del(Long... ids) {
		for (Long id : ids) {
			if (notExist(id)) {
				continue;
			}
			try {
				businessTeamDao.delete(id);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
				throw new BusinessException("发生异常!");
			}
		}
	}

	private boolean notExist(Long id) {
		return get(id) == null;
	}

	public Page<BusinessTeam> search(Page<BusinessTeam> page, BusinessTeamCriteria criteria) {
		try {
			return businessTeamDao.search(page, criteria);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}
}
